//
//  DHUser_HomePageController.m
//  Dresshere
//
//  Created by ZhangPeng on 2018/6/13.
//  Copyright © 2018年 张朋. All rights reserved.
//

#import "DHUser_HomePageController.h"
#import "DHUser_HomePage_HeadCell.h"
#import "DHUser_HomePage_CommunityCell.h"
#import "DHUser_HomePage_FollowCell.h"

@interface DHUser_HomePageController ()<UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) NSMutableArray *itemArr;
@property (assign, nonatomic) NSInteger pageIndex;

@property (assign, nonatomic) NSInteger index;
@property (strong, nonatomic) NSString *keyword;

@end

@implementation DHUser_HomePageController

- (NSMutableArray *)itemArr{
    if (!_itemArr) {
        _itemArr = [NSMutableArray array];
    }
    return _itemArr;
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.tableView reloadData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.titleLabel.text = @"社交主页";
    
    self.index = 0;
    self.keyword = nil;
    
    [self.tableView registerNib:[UINib nibWithNibName:@"DHUser_HomePage_HeadCell" bundle:nil] forCellReuseIdentifier:kDHUser_HomePage_HeadCell];
    [self.tableView registerNib:[UINib nibWithNibName:@"DHUser_HomePage_CommunityCell" bundle:nil] forCellReuseIdentifier:kDHUser_HomePage_CommunityCell];
    [self.tableView registerNib:[UINib nibWithNibName:@"DHUser_HomePage_FollowCell" bundle:nil] forCellReuseIdentifier:kDHUser_HomePage_FollowCell];
    self.tableView.mj_footer = [DHMJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    
    [self loadData];
}

- (void)routerEventWithName:(NSString *)eventName userInfo:(NSDictionary *)userInfo{
    if ([eventName isEqualToString:@"tab_1"]) {
        self.index = 0;
        [self loadData];
    }else if ([eventName isEqualToString:@"tab_2"]){
        self.index = 1;
        [self loadData];
    }else if ([eventName isEqualToString:@"tab_3"]){
        self.index = 2;
        [self loadData];
    }
}

- (void)loadData{
    [self.tableView.mj_footer resetNoMoreData];
    if (self.index == 0) {
        self.pageIndex = 0;
        [self.itemArr removeAllObjects];
        [[DHNetAPIClient shareDHNetAPIClient] request_social_postsWithParams:@{@"pageIndex":@(self.pageIndex),@"pageSize":@(20)} andBlock:^(id data, NSError *error) {
            [self.tableView.mj_header endRefreshing];
            if (data) {
                [self.itemArr addObjectsFromArray:[data objectForKey:@"posts"]];
                if (self.itemArr.count < 20) {
                    [self.tableView.mj_footer endRefreshingWithNoMoreData];
                }else{
                    self.pageIndex += 1;
                }
                [self.tableView reloadData];
            }
        }];
    }else if (self.index == 1){
        self.pageIndex = 0;
        [self.itemArr removeAllObjects];
        NSDictionary *params;
        if (self.keyword) {
            params = @{@"name":self.keyword,@"pageIndex":@(self.pageIndex),@"pageSize":@(20)};
        }else{
            params = @{@"pageIndex":@(self.pageIndex),@"pageSize":@(20)};
        }
        [[DHNetAPIClient shareDHNetAPIClient] request_social_fansWithParams:params andBlock:^(id data, NSError *error) {
            [self.tableView.mj_header endRefreshing];
            if (data) {
                [self.itemArr addObjectsFromArray:[data objectForKey:@"fans"]];
                if (self.itemArr.count < 20) {
                    [self.tableView.mj_footer endRefreshingWithNoMoreData];
                }else{
                    self.pageIndex += 1;
                }
                [self.tableView reloadData];
            }
        }];
    }else{
        self.pageIndex = 0;
        [self.itemArr removeAllObjects];
        NSDictionary *params;
        if (self.keyword) {
            params = @{@"name":self.keyword,@"pageIndex":@(self.pageIndex),@"pageSize":@(20)};
        }else{
            params = @{@"pageIndex":@(self.pageIndex),@"pageSize":@(20)};
        }
        [[DHNetAPIClient shareDHNetAPIClient] request_social_subscribedWithParams:params andBlock:^(id data, NSError *error) {
            [self.tableView.mj_header endRefreshing];
            if (data) {
                [self.itemArr addObjectsFromArray:[data objectForKey:@"subscribed"]];
                if (self.itemArr.count < 20) {
                    [self.tableView.mj_footer endRefreshingWithNoMoreData];
                }else{
                    self.pageIndex += 1;
                }
                [self.tableView reloadData];
            }
        }];
    }
}

- (void)loadMoreData{
    if (self.index == 0) {
        [[DHNetAPIClient shareDHNetAPIClient] request_social_postsWithParams:@{@"pageIndex":@(self.pageIndex),@"pageSize":@(20)} andBlock:^(id data, NSError *error) {
            [self.tableView.mj_footer endRefreshing];
            if (data) {
                [self.itemArr addObjectsFromArray:[data objectForKey:@"posts"]];
                if (self.itemArr.count < (self.pageIndex + 1) * 20) {
                    [self.tableView.mj_footer endRefreshingWithNoMoreData];
                }else{
                    self.pageIndex += 1;
                }
                [self.tableView reloadData];
            }
        }];
    }else if (self.index == 1){
        NSDictionary *params;
        if (self.keyword) {
            params = @{@"name":self.keyword,@"pageIndex":@(self.pageIndex),@"pageSize":@(20)};
        }else{
            params = @{@"pageIndex":@(self.pageIndex),@"pageSize":@(20)};
        }
        [[DHNetAPIClient shareDHNetAPIClient] request_social_fansWithParams:params andBlock:^(id data, NSError *error) {
            [self.tableView.mj_footer endRefreshing];
            if (data) {
                [self.itemArr addObjectsFromArray:[data objectForKey:@"fans"]];
                if (self.itemArr.count < (self.pageIndex + 1) * 20) {
                    [self.tableView.mj_footer endRefreshingWithNoMoreData];
                }else{
                    self.pageIndex += 1;
                }
                [self.tableView reloadData];
            }
        }];
    }else{
        NSDictionary *params;
        if (self.keyword) {
            params = @{@"name":self.keyword,@"pageIndex":@(self.pageIndex),@"pageSize":@(20)};
        }else{
            params = @{@"pageIndex":@(self.pageIndex),@"pageSize":@(20)};
        }
        [[DHNetAPIClient shareDHNetAPIClient] request_social_subscribedWithParams:params andBlock:^(id data, NSError *error) {
            [self.tableView.mj_footer endRefreshing];
            if (data) {
                [self.itemArr addObjectsFromArray:[data objectForKey:@"subscribed"]];
                if (self.itemArr.count < (self.pageIndex + 1) * 20) {
                    [self.tableView.mj_footer endRefreshingWithNoMoreData];
                }else{
                    self.pageIndex += 1;
                }
                [self.tableView reloadData];
            }
        }];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UITableViewDataSource and UITableViewDelegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 2;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        return 250;
    }else{
        if (self.index == 0) {
            if (self.itemArr.count > 0) {
                __block CGFloat totalHeight = 0;
                [self.itemArr enumerateObjectsUsingBlock:^(NSDictionary *model, NSUInteger idx, BOOL * _Nonnull stop) {
                    NSArray *images = [model objectForKey:@"images"];
                    
                    //计算文字高度
                    CGFloat contentHeight = [[model objectForKey:@"content"] heightForFont:[UIFont systemFontOfSize:14] width:kScreenWidth - 20];
                    //如果内容中有 \n 等转义字符
                    NSInteger specialChar = 0;//出现次数
                    specialChar = [[model objectForKey:@"content"] countOccurencesOfString:@"\n"];
                    CGFloat textHeight = contentHeight + specialChar * 14;
                    
                    //图片CollectionView高度
                    CGFloat imgsHeight = 0;
                    if (images.count == 0) {
                        imgsHeight = 0;
                    }else if (images.count > 0 && images.count <= 3){
                        imgsHeight = 128;
                    }else if (images.count > 3 && images.count <= 6){
                        imgsHeight = 128 * 2 + 5;
                    }else if (images.count > 6 && images.count <= 9){
                        imgsHeight = 128 * 3 + 10;
                    }
                    
                    totalHeight += imgsHeight + textHeight + 85;
                }];
                return totalHeight;
            }else{
                self.tableView.mj_footer.hidden = YES;
                return 150;
            }
        }else if (self.index == 1){
            return 56 + self.itemArr.count * 60;
        }else{
            return 56 + self.itemArr.count * 60;
        }
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        DHUser_HomePage_HeadCell *cell = [tableView dequeueReusableCellWithIdentifier:kDHUser_HomePage_HeadCell forIndexPath:indexPath];
        [cell updateCell:nil];
        return cell;
    }else{
        if (self.index == 0) {
            DHUser_HomePage_CommunityCell *cell = [tableView dequeueReusableCellWithIdentifier:kDHUser_HomePage_CommunityCell forIndexPath:indexPath];
            [cell updateCellArr:self.itemArr];
            if (self.itemArr.count > 0) {
                cell.hindLabel.hidden = YES;
            }else{
                cell.hindLabel.hidden = NO;
            }
            return cell;
        }else if (self.index == 1){
            DHUser_HomePage_FollowCell *cell = [tableView dequeueReusableCellWithIdentifier:kDHUser_HomePage_FollowCell forIndexPath:indexPath];
            [cell updateCellArr:self.itemArr];
            cell.isFans = NO;
            return cell;
        }else{
            DHUser_HomePage_FollowCell *cell = [tableView dequeueReusableCellWithIdentifier:kDHUser_HomePage_FollowCell forIndexPath:indexPath];
            [cell updateCellArr:self.itemArr];
            cell.isFans = YES;
            return cell;
        }
    }
}

@end
